package com.starsoft.smdc.crawler.sina;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.JsonNode;
import com.starsoft.frame.fileimport.FileReader;
import com.starsoft.frame.fileimport.JsonReader;
import com.starsoft.frame.fileimport.pojo.Column;
import com.starsoft.frame.fileimport.pojo.DataTable;
import com.starsoft.frame.fileimport.pojo.NullType;
import com.starsoft.frame.fileimport.pojo.Table;
import com.starsoft.frame.fileimport.pojo.Table.TitleType;
import com.starsoft.frame.fileimport.pojo.ValueType;
import com.starsoft.frame.util.MapUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcDiv;
import com.starsoft.smdc.bean.SmdcDivId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.common.DivCrawler;
import com.starsoft.smdc.dao.DivDao;
import com.starsoft.smdc.util.SecurityType;

public class DivSina extends SinaService implements DivCrawler{

	private static Logger logger = LoggerFactory.getLogger(DivSina.class);
	
	@Autowired
	DivDao divDao;

	@Override
	public List<SmdcDiv> update(SmdcSecurity security, Date beginDate) {
		return update(Arrays.asList(security), Arrays.asList(beginDate));
	}
	
	@Override
	public List<SmdcDiv> update(List<SmdcSecurity> securityList, List<Date> beginDates) {
		List<SmdcDiv> result = new ArrayList<SmdcDiv>();
		for (int i=0;i< securityList.size();i++) {
			SmdcSecurity security = securityList.get(i);
			if(SecurityType.isFund(security.getSecType())){
				result.addAll(getFundDiv(security, beginDates.get(i)));
			}
		}
		for (SmdcDiv div : result) {
			divDao.saveOrUpdate(div);
		}
		return result;
	}

	private List<SmdcDiv> getFundDiv(SmdcSecurity security, Date beginDate){
		String apiName = "fundDiv";
		String[] params = { security.getTicker(), String.valueOf(new Date().getTime()) };

		List<SmdcDiv> divList = new ArrayList<SmdcDiv>();
		String response = this.request(apiName, params);
		if(StringUtil.isEmpty(response) || StringUtil.isEmpty(response)){
			return divList;
		}
		JsonNode responseJsonPageOne = getJsonNode(response);
		JsonNode divNode = responseJsonPageOne.get("result").get("data").get("fhdata");
		if(divNode == null || divNode.isNull()){
			return divList;
		}
		
		List<Column> columnList = new ArrayList<Column>();
		FileReader reader=new JsonReader();
		columnList.add(new Column("djr", "publishDate", ValueType.Date, NullType.nullable, "yyyy-MM-dd HH:mm:ss"));
		columnList.add(new Column("mffh", "perCashDiv", ValueType.Double, NullType.nullable));
		DataTable ob= reader.read(divNode.toString(), new Table(TitleType.OnlyCode,columnList));
		
		for (Map<String, Object> mapObject : ob.getData()) {
			try {
				Date publishDate = (Date) mapObject.get("publishDate");
				if (publishDate==null || publishDate.before(beginDate)) {
					continue;
				}
				Date divDate = holidayService.getNextBusinessDay(publishDate, 1);
				SmdcDivId id = new SmdcDivId(security.getSecId(), divDate);
				SmdcDiv divInfo = divDao.get(id);
				if (divInfo == null) {
					divInfo = new SmdcDiv(id);
				}
				MapUtil.updateBean(divInfo, mapObject);
				divList.add(divInfo);
			} catch (Exception e) {
				logger.error("map to bean error " + mapObject.get("secId"), e);
			}
		}
		return divList;
	}

	@Override
	public List<SmdcDiv> update(InputStream is) {
		throw new RuntimeException("DivSina not support this method");
	}
	
}
